import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;



import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.Color;


public class FindTutors extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table = new JTable();

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public FindTutors(final String user) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.ORANGE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		JLabel lblNewLabel_5 = new JLabel("");
		lblNewLabel_5.setIcon(new ImageIcon(LogIn.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		lblNewLabel_5.setBounds(600, 30, 201, 164);
		getContentPane().add(lblNewLabel_5);
		
		JLabel lblCourseCode = new JLabel("Course Code:");
		lblCourseCode.setBounds(22, 19, 90, 16);
		contentPane.add(lblCourseCode);
		
		textField = new JTextField();
		textField.setBounds(124, 13, 134, 28);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblEnterKeyword = new JLabel("Enter Keyword:");
		lblEnterKeyword.setBounds(317, 19, 102, 16);
		contentPane.add(lblEnterKeyword);
		
		textField_1 = new JTextField();
		textField_1.setBounds(431, 13, 134, 28);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblOr = new JLabel("OR");
		lblOr.setBounds(273, 19, 32, 16);
		contentPane.add(lblOr);
		
		JButton btnFindTutors = new JButton("Find Tutors");
		btnFindTutors.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					Connection con = Connect.getConnection();
					Statement stat =con.createStatement();
					ResultSet re=null;
					boolean check = false;
					if (!textField.getText().equals("")|| !(textField.getText().trim().length() ==0)){
						
						re = stat.executeQuery("SELECT C.Code, C.Course_Title, S.Name, S.Email_ID FROM CRS_CODE AS C,STUDENT AS S,TUTORS_FOR AS TF,TUTOR AS T WHERE C.Code='"+textField.getText()+"' "+ " AND C.Course_Title=TF.Course_Title AND TF.Tutor_ID = T.Tutor_Identification AND T.Student_Username=S.Username");
					 // while(re.next()){
						  check=true;
					  //}
					}
					else if(!textField_1.getText().equals("")|| !(textField_1.getText().trim().length() ==0)){
					
						re = stat.executeQuery("SELECT C.Code, C.Course_Title, S.Name, S.Email_ID FROM CRS_CODE AS C, STUDENT AS S, TUTORS_FOR AS TF, TUTOR AS T WHERE C.Course_Title LIKE '%" +textField_1.getText() +"%' AND C.Course_Title=TF.Course_Title AND TF.Tutor_ID = T.Tutor_Identification AND T.Student_Username=S.Username");	
					//while(re.next()){
						check=true;
					//}
					}
					if (check==false){
						JOptionPane.showMessageDialog(null,"No Results Found");
					}
					else{
						Object[] columns = {"<html><b>Code</b><html>",
				                "<html><b>Course Title</b></html>",
				                "<html><b>Tutor Name</b></html>",
				                "<html><b>Tutor Email</b></html>"
				                };
						ResultSetMetaData metaData = re.getMetaData();	
						int numberOfColumns = metaData.getColumnCount();
						int r=0;
						ArrayList<Object[]> ro = new ArrayList<Object[]>();
						while(re.next()){
							Object[] newRow = new Object[numberOfColumns];

			                for (int i = 1; i <= numberOfColumns; i++) {
			                	 
			                    newRow[i-1]=re.getObject(i);
			                    System.out.println(re.getObject(i));
			                }
			                ro.add(newRow);
			                r++;
						}
						Object[][] rows = new Object[r+1][numberOfColumns];
						
						rows[0] = columns;
						for(int i=1;i<=r;i++){
							
							rows[i]= ro.get(i-1);
						}
						DefaultTableModel model=new DefaultTableModel(rows, columns);
						
						
						contentPane.remove(table);
						table = new JTable(model);
						contentPane.add(table);
						table.setBounds(23, 200, 700, 400);
						refresh();
					}
			}catch(Exception e){
				JOptionPane.showMessageDialog(contentPane,"Check Connection");
				
			}
			}});
		btnFindTutors.setBounds(610, 14, 117, 29);
		contentPane.add(btnFindTutors);
		
		JButton btnStudentServices = new JButton("Student Services");
		btnStudentServices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				new StudentServices(user);
			}
		});
		btnStudentServices.setBounds(610, 195, 134, 29);
		contentPane.add(btnStudentServices);
		setPreferredSize(new Dimension(800, 500));
		pack();
		setVisible(true);
		setTitle("Find Tutors");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
public void refresh()
	
    {
		
        getRootPane().validate();
        getRootPane().repaint();
        this.pack();
    }
}
